package com.xpalive.config.spring.exceptionhandle;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.MessageFormat;

/**
 * 自定义异常处理器，
 * 继承默认的异常处理器，并在处理前将异常信息添加到request中
 */
@Component
public class GalaxyHandlerExceptionResolver extends DefaultHandlerExceptionResolver {

    @Override
    public ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response,
                                           Object handler, Exception ex) {
        request.setAttribute("exception", MessageFormat.format("{0}:{1}", ex.getClass().getCanonicalName(), ex.getMessage()));
        return super.doResolveException(request, response, handler, ex);
    }
}
